package com.yuchting.dinneronline.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.yuchting.dinneronline.client.common.CommonDialog;
import com.yuchting.dinneronline.client.common.RankPanel;

public class MainPage implements EntryPoint {

	public final static MainServiceAsync fsm_server = GWT.create(MainService.class);
	
	private final class LoginDialog extends DialogBox{
		public TextBox	m_name			= new TextBox();
		public PasswordTextBox m_pass	= new PasswordTextBox();
		
		public LoginDialog(){
			
			VerticalPanel t_pane = new VerticalPanel();
			t_pane.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			
			HorizontalPanel t_user = new HorizontalPanel();
			t_user.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
			t_user.add(new HTML("名字: "));
			t_user.add(m_name);
			
			t_pane.add(t_user);
			
			HorizontalPanel t_pass = new HorizontalPanel();
			t_pass.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
			t_pass.add(new HTML("密码: "));
			t_pass.add(m_pass);
			
			t_pane.add(t_pass);
		
			final Button t_confirm = new Button("登陆");
			t_confirm.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					if(m_name.getText().isEmpty() 
					|| m_pass.getText().isEmpty()){
						
						return ;
					}
					
					CommonDialog.PopupWaiting("登陆中...");
					try{
						fsm_server.logonServer(m_name.getText(),m_pass.getText(), new AsyncCallback<String>() {
							
							@Override
							public void onSuccess(String result) {
								CommonDialog.HideWaiting();
								
								if(result.startsWith("<Error>")){
									CommonDialog.PopupPrompt(result, LoginDialog.this);
									return ;
								}
								
								hide();
								
								try{
									m_mainPanel.show(result);
								}catch(Exception e){
									CommonDialog.PopupPrompt("exception :" + e.getMessage(),LoginDialog.this);
								}															
							}
							
							@Override
							public void onFailure(Throwable caught) {
								CommonDialog.PopupPrompt(caught.getMessage(), LoginDialog.this);
								CommonDialog.HideWaiting();
							}
						});
					}catch(Exception e){
						CommonDialog.PopupPrompt(e.getMessage(), LoginDialog.this);
						CommonDialog.HideWaiting();
					}
					
				}
			});
			t_pane.add(t_confirm);
			
			m_pass.addKeyUpHandler(new KeyUpHandler() {
				
				@Override
				public void onKeyUp(KeyUpEvent event) {
					if(event.getNativeKeyCode() ==  KeyCodes.KEY_ENTER){
						t_confirm.click();
					}				
				}
			});
			
			setWidget(t_pane);
			setText("菜单系统登录");
			setAnimationEnabled(true);
			center();	
			
			m_name.setFocus(true);
		}
		
		
	}
	
	public MainPanel m_mainPanel = new MainPanel(this);
		
	public void onModuleLoad() {
		new LoginDialog().setVisible(true);
	}
	

}
